package com.sevenyear.excelanalyze.listener.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sevenyear.excelanalyze.common.utils.CalculateUtils;
import com.sevenyear.excelanalyze.entity.HydroelectricConversionRate;
import com.sevenyear.excelanalyze.entity.IrrigationReturnflowRate;
import com.sevenyear.excelanalyze.entity.PartitionBaseInfo;
import com.sevenyear.excelanalyze.service.IrrigationReturnflowRateService;
import com.sevenyear.excelanalyze.service.PartitionBaseInfoService;

/**
 * @author ：SevenYear
 * @description：TODO
 * @date ：2021/8/29 22:22
 */
public class IrrigationReturnflowRateListener extends AnalysisEventListener<IrrigationReturnflowRate> {

    private IrrigationReturnflowRateService irrigationReturnflowRateService;
    private PartitionBaseInfoService partitionBaseInfoService;

    public IrrigationReturnflowRateListener() {
    }

    public IrrigationReturnflowRateListener(IrrigationReturnflowRateService irrigationReturnflowRateService, PartitionBaseInfoService partitionBaseInfoService) {
        this.irrigationReturnflowRateService = irrigationReturnflowRateService;
        this.partitionBaseInfoService = partitionBaseInfoService;
    }

    @Override
    public void invoke(IrrigationReturnflowRate irrigationReturnflowRate, AnalysisContext analysisContext) {
        //计算平均值
        float average = CalculateUtils.average(irrigationReturnflowRate.getIrrigationReturnflowRateMin(), irrigationReturnflowRate.getIrrigationReturnflowRateMax());
        irrigationReturnflowRate.setIrrigationReturnflowRateAvg(average);
        //设置分区主键id,分区编码partitionCode,区域编码areaCode
        QueryWrapper<PartitionBaseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("landform", irrigationReturnflowRate.getLandform()).eq("clay_thickness", irrigationReturnflowRate.getClayThickness());
        PartitionBaseInfo partitionBaseInfo = partitionBaseInfoService.getOne(wrapper);
        irrigationReturnflowRate.setPartitionId(partitionBaseInfo.getId());
        irrigationReturnflowRate.setPartitionCode(partitionBaseInfo.getPartitionCode());
        irrigationReturnflowRate.setAreaCode(irrigationReturnflowRate.getArea() + irrigationReturnflowRate.getPartitionCode());
        UpdateWrapper<IrrigationReturnflowRate> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("area_code", irrigationReturnflowRate.getAreaCode());
        irrigationReturnflowRateService.saveOrUpdate(irrigationReturnflowRate, updateWrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
